package com.yqRecite.controller;
import com.yqRecite.entity.*;
import com.yqRecite.service.*;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import com.yqRecite.util.util.ImgVideoUploadUtil;
import com.yqRecite.vo.AllPeople;
import com.yqRecite.vo.LoginUser;
import com.yqRecite.vo.RegisterUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("public")
public class PublicController {
    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;
    @Resource
    private CommentsService commentsService;


    /**
     * 登录
     * @param loginUser
     * @return
     */
    @RequestMapping("login")
    Result login(@RequestBody LoginUser loginUser){
        System.out.println("user: "+loginUser.getRole());
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        String role = loginUser.getRole();
        if (role.equals("user")){
            return userService.login(username, password);
        }else if(role.equals("admin")){
            return adminService.login(username, password);
        }
        return ResultUtil.error("无此用户", loginUser);
    }

    /**
     * 登录
     * @param registerUser
     * @return
     */
    @RequestMapping("register")
    Result login(@RequestBody RegisterUser registerUser){
        System.out.println("user: "+registerUser);
        String role = registerUser.getRole();
        if (role.equals("user")){
            return userService.register(registerUser);
        }else if(role.equals("admin")){
            return ResultUtil.fail("暂时还未开通管理员入口", null);
        }
        return ResultUtil.error("无此角色", registerUser);
    }

    @RequestMapping("uploadImg")
    public Result updateAvatar(@RequestParam("file") MultipartFile file, @RequestParam Map<String, String> params, HttpServletRequest request) throws IOException, ParseException {
        ImgVideoUploadUtil imgVideoUploadUtil = new ImgVideoUploadUtil();
        AllPeople people = new AllPeople();
//        System.out.println(people);
        people.setId(Integer.parseInt(params.get("id")));
        people.setYqId(params.get("yqId"));
        people.setAvatar(params.get("avatar"));
        people.setUsername(params.get("username"));
        people.setRole(params.get("role"));
        people.setPassword(params.get("password"));
        if (people.getRole().equals("user")){//普通用户才有下面的字段
            people.setEmail(params.get("email"));
            people.setEnable(Integer.parseInt(params.get("enable")));
            people.setGender(params.get("gender"));
            people.setPhone(params.get("phone"));
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 指定日期格式
            Date date = dateFormat.parse(params.get("createTime"));
            people.setCreateTime(date);
            people.setBgImgUrlId(params.get("bgImgUrlId"));
            people.setBannerList(new String[]{String.join(",", params.get("bannerList"))});
            people.setSignature(params.get("signature"));
        }
        System.out.println("file==>"+file);
        System.out.println("people==>"+people);
        String avatar = imgVideoUploadUtil.upload(file, request);
        if (avatar != null){
            if (people.getRole().equals("admin")){
                Admin admin = new Admin();
                admin.setAvatar(avatar);
                admin.setId(people.getId());
                admin.setUsername(people.getUsername());
                admin.setRole(people.getRole());
                admin.setPassword(people.getPassword());
                return adminService.updateAdmin(admin);
            }else if (people.getRole().equals("user")){
                User user = new User();
                user.setAvatar(avatar);
                user.setId(people.getId());
                user.setUsername(people.getUsername());
                user.setEnable(people.getEnable());
                user.setCreateTime(people.getCreateTime());
                user.setPassword(people.getPassword());
                user.setRole(people.getRole());
                user.setEmail(people.getEmail());
                user.setPhone(people.getPhone());
                user.setGender(people.getGender());
                user.setBgImgUrlId(people.getBgImgUrlId());
                user.setBannerList(String.join(",", people.getBannerList()));
                user.setSignature(people.getSignature());
                int res = userService.updateUser(user);
                if (res == 1){
                    System.out.println("图片上传成功, user==>" + user);
                    return ResultUtil.ok(user);
                }else {
                    return ResultUtil.error("图片上传出现错误！", avatar);
                }
            }else {
                return ResultUtil.error("用户数据错误！", people);
            }
        }else{
            return ResultUtil.error("图片上传出现错误！", avatar);
        }
    }

    @RequestMapping("prepareUpload")
    public Result prepareUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        ImgVideoUploadUtil imgVideoUploadUtil = new ImgVideoUploadUtil();
        System.out.println("file==>"+file);
        String avatar = imgVideoUploadUtil.upload(file, request);
        if (avatar != null){
            System.out.println("预上传的图片为==>" + avatar);
            return ResultUtil.ok(avatar);
        }else{
            return ResultUtil.error("图片上传出现错误！", file);
        }
    }

    @RequestMapping("updatePeople")
    Result updatePeople(@RequestBody AllPeople allPeople){
        System.out.println("allPeople: " + allPeople);
        if (allPeople.getRole().equals("admin")){
            Admin admin = new Admin();
            admin.setId(allPeople.getId());
            admin.setYqId(allPeople.getYqId());
            admin.setUsername(allPeople.getUsername());
            admin.setPassword(allPeople.getPassword());
            admin.setRole(allPeople.getRole());
            admin.setAvatar(allPeople.getAvatar());
            return adminService.updateAdmin(admin);
        }else if (allPeople.getRole().equals("user")){
            User person = new User();
            person.setId(allPeople.getId());
            person.setYqId(allPeople.getYqId());
            person.setRole(allPeople.getRole());
            person.setUsername(allPeople.getUsername());
            person.setCreateTime(allPeople.getCreateTime());
            person.setPassword(allPeople.getPassword());
            person.setAvatar(allPeople.getAvatar());
            person.setGender(allPeople.getGender());
            person.setEmail(allPeople.getEmail());
            person.setEnable(allPeople.getEnable());
            person.setPhone(allPeople.getPhone());
            if (allPeople.getBannerList() != null){
                person.setBannerList(String.join(",", allPeople.getBannerList()));
            }
            person.setBgImgUrlId(allPeople.getBgImgUrlId());
            person.setSignature(allPeople.getSignature());
            System.out.println("user==> "+person);
            int res = userService.updateUser(person);
            if(res == 1){
                return ResultUtil.ok(allPeople);
            }else{
                return ResultUtil.fail("修改失败", allPeople);
            }
        } else {
            return ResultUtil.fail("角色有问题", allPeople);
        }
    }

    /**
     * 获取所有评论
     * @return
     */
    @RequestMapping("getAllComments")
    public Result getAllComments(){
        return commentsService.getAllCommentList();
    }

    /**
     * 获取所有此化妆品的评论
     * @return
     */
    @RequestMapping("getAllCommentsByCid/{id}")
    public Result getAllCommentsByCid(@PathVariable("id") int id){
        return commentsService.getAllCommentListByCid(id);
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @RequestMapping("delComment/{id}")
    public Result delComment(@PathVariable("id") int id){
        return commentsService.deleteComment(id);
    }

    /**
     * 增加一条新的评论
     * @param newOne
     * @return
     */
    @RequestMapping("addNewComment")
    public Result addNewComment(@RequestBody Comments newOne){
        newOne.setCreateTime(new Date());
        return commentsService.addOneComment(newOne);
    }
}
